package com.zqh.myfmmall.WxPay;

import com.github.wxpay.sdk.WXPay;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-01-28 1:14
 */
@Service("wxPayService")
public class WxPayServiceImpl implements WxPayService {
    @Override
    public String wxPay( Map<String,String> orderinfoMap) throws Exception{
        Map<String, String> data = new HashMap<>();
        //商品描述
        data.put("body",orderinfoMap.get("productNames"));
        //使⽤当前⽤户订单的编号作为当前⽀付交易的交易号
        data.put("out_trade_no",orderinfoMap.get("orderId"));
        //⽀付币种
        data.put("fee_type","CNY");
        BigDecimal actualAmount = new BigDecimal(orderinfoMap.get("actualAmount"));
        //⽀付⾦额
        data.put("total_fee",actualAmount.multiply(new BigDecimal(100)).toString());
        //交易类型
        data.put("trade_type","NATIVE");
        //设置⽀付完成时的回调⽅法接⼝
        data.put("notify_url","http://120.24.54.47:8081/pay/success");
        WXPay wxPay = new WXPay(new MyPayConfig());
        //发送请求，获取响应
        //微信⽀付：申请⽀付连接
        Map<String, String> resp = null;
            resp = wxPay.unifiedOrder(data);
            return resp.get("code_url");
    }

    @Override
    public boolean checkPayed(String orderId) throws Exception {
        WXPay wxPay = new WXPay(new MyPayConfig());
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("out_trade_no",orderId);
        Map<String, String> resp = wxPay.orderQuery(paramsMap);
        return "success".equalsIgnoreCase(resp.get("trade_state"));
    }
}
